<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\CityArea;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->number('序号');
            $grid->column('name');
            $grid->column('account');
            // $grid->column('password');
            $grid->column('agent','代理人手机号');
            // $grid->column('store_id');
            // $grid->column('store_appId');
            $grid->column('store_address');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('phone');
            $show->field('store_id');
            $show->field('store_appId');
            $show->field('app_token');
            $show->field('store_sys_code');

            $show->title('地区')->as(function ($title) {
                $province_name =  DB::table('city_area')->where('id',$this->province_id)->value('name');
                $cityName = DB::table('city_area')->where('id',$this->city_id)->value('name');
                $districtName = DB::table('city_area')->where('id',$this->district_id)->value('name');
                $steeetName = DB::table('city_area')->where('id',$this->street_id)->value('name');

                return $province_name.'/'.$cityName.'/'.$districtName.'/'.$steeetName;
            });

            $show->field('store_address');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('account','店铺账号');
            $form->password('password');
            $form->text('phone','店铺手机号');
            $form->text('store_id','店铺ID');
            $form->text('store_appId','店铺爱宝appid');
            $form->text('app_token','爱宝apptoken');
            $form->text('store_sys_code','爱宝店铺编号');

            // 省份
            $form->width(4)->select('province_id','省')->options(CityArea::where('parent_id','')->get()->pluck('name', 'id'))->rules('required')->load('city_id', '/user/city');
            // 城市
            $form->width(4)->select('city_id','市')->load('district_id', '/user/city');
            // 区域
            $form->width(4)->select('district_id','区')->load('street_id','/user/city');
            //街道
            $form->width(4)->select('street_id','街道');

            $form->text('store_address','店铺详细地址');
            $form->text('agent','代理人手机号');
            $form->rate('agent_proportion','代理分润比例');



            $form->submitted(function (Form $form) {
            if($form->password != $form->model()->password){
                $form->password = md5(md5($form->password));
            }

        });

        });
    }


   public function city(Request $request)
   {
        $provinceId = $request->get('q');
        return CityArea::where('parent_id', $provinceId)->get(['id', 'name as text']);
   }


}
